package com.jhws.sys.menu.controller;


import java.util.List;

import javax.annotation.Resource;

import com.jhws.sys.user.bo.UserPerms;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.jsonUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jhws.sys.menu.bo.TblMenuDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.util.GetId;
import com.jhws.sys.menu.service.ITblMenuDimService;


@Controller
@RequestMapping("/menu")
public class MenuCtrl
{
	@Resource
	ITblMenuDimService IMenuService;
	@Resource
    ITblUserDimService IUserService;
	@RequestMapping("query.x")
	@ResponseBody
	@RequiresRoles("admin")
	public JsonRespData query() throws Exception {
		List<TblMenuDimBo> list = IMenuService.findTblMenuDimByWhere("where 1=1");
		return new JsonRespData("",1,list);
	}
	
	@RequestMapping("delete.x")
	@ResponseBody
	public JsonRespData delete(String menuCode) {
		IMenuService.removeMenuDim(menuCode);
		return new JsonRespData("",1,"");
	}
	
	@RequestMapping("forSave.x")
	@RequiresRoles("admin")
	public ModelAndView forSave(String menuCode) throws Exception{
		ModelAndView mv = new ModelAndView();
		if(menuCode != null) {
			TblMenuDimBo bo = IMenuService.findTblMenuDimById(menuCode);
			List<TblMenuDimBo> parentList = IMenuService.findTblMenuDimByWhere("where 1=1 and menuCode ='"+bo.getMenuPar()+"'");
			mv.addObject("defMenu", bo);
			mv.addObject("defMenuPar", parentList);
		}
		
		mv.setViewName("sys_mng/menu/forSave");
		return mv;
	}
	
	@RequestMapping("save.x")
	@ResponseBody
	public JsonRespData save(TblMenuDimBo bo) throws Exception{
		if(bo.getMenuCode() == null || bo.getMenuCode().isEmpty())
			bo.setMenuCode(GetId.randomID());

		IMenuService.save(bo);
		return new JsonRespData("",1,null);
	}

	/**
	 * 获取某个level之前的所有level的菜单数据
	 * @param level
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("getMenuByLevelBefore.x")
	@ResponseBody
	public JsonRespData getMenuByLevel(int level) throws Exception {
		List<TblMenuDimBo> menuList = IMenuService.findTblMenuDimByWhere("where 1=1 and menuLev < '"+level+"'");
		return new JsonRespData("",1,menuList);
	}

	@RequestMapping("getMenu.x")
    @ResponseBody
    public JsonRespData getMenu(String menuCode) throws Exception {
	    TblMenuDimBo menu = IMenuService.findTblMenuDimById(menuCode);
	    return new JsonRespData("",1,menu);
    }


    @RequestMapping("saveUserPerms.x")
	@ResponseBody
	public JsonRespData saveUserPerms(String userCode,String perms){
        List<TblMenuDimBo>list = jsonUtil.getBeansFromJsonArray(perms,TblMenuDimBo.class);
        for(TblMenuDimBo menu : list){
            UserPerms userPerms = new UserPerms();
            userPerms.setMenuCode(menu.getMenuCode());
            userPerms.setUserCode(userCode);
            IUserService.saveUserPerms(userPerms);
        }
        return new JsonRespData("",1,"");
	}
}
